package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.Article;
import com.ruoyi.domain.Danger;
import com.ruoyi.mapper.ArticleMapper;
import com.ruoyi.mapper.DangerMapper;
import com.ruoyi.service.ArticleService;
import com.ruoyi.service.IDangerService;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class DangerService extends ServiceImpl<DangerMapper, Danger> implements IDangerService {
    @Autowired
    private  DangerMapper dangerMapper;
    @Autowired
    private FileStorageService fileStorageService;//注入实列

    @Override
    public List<Danger> selectAll() {
        return dangerMapper.selectList(null);
    }
    @Override
    public Danger selectById(Integer id) {
        LambdaQueryWrapper<Danger> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Danger::getId,id);
        return dangerMapper.selectOne(lambdaQueryWrapper);
    }

    public AjaxResult uploadFile(int id, MultipartFile file) throws Exception {
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return AjaxResult.error("上传文件为空");
            }

            // 获取 base-path
            String basePath = "articleFilePath/"; // 直接在这里设置基础路径，或者从配置文件中读取

            // 指定oss保存文件路径
            String objectName = basePath + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + "/" + file.getOriginalFilename();
            // 上传图片，成功返回文件信息
            FileInfo fileInfo = fileStorageService.of(file).setPath(objectName).upload();

            // 设置返回结果
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", fileInfo.getUrl());
            ajax.put("fileName", fileInfo.getUrl());  // 注意：这里的值要改为url，前端访问的地址,需要文件的地址 而不是文件名称
            ajax.put("newFileName", fileInfo.getUrl());
            ajax.put("originalFilename", file.getOriginalFilename());

            // 创建Article对象并设置ID和filePath
//            Article article = new Article();
//            article.setArticleId(id); // 你需要实现一个方法来生成唯一的ID，或者从其他地方获取ID
//            article.setCoverPath(fileInfo.getUrl());
            Danger danger = new Danger();
            danger.setId(id); // 你需要实现一个方法来生成唯一的ID，或者从其他地方获取ID
            danger.setVideo(fileInfo.getUrl());

            // 更新数据库中的filePath字段
            Integer result = dangerMapper.insert(danger);
            if (result<=0) {
                return AjaxResult.error("插入文件路径失败");
            }
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
